package com.video.controller.webcon;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.entity.ResultData;
import com.video.commons.loginaop.LoginUser;
import com.video.commons.loginaop.MustLogin;
import com.video.commons.runtimelog.RequestUnLog;
import com.video.commons.sendaop.SocketSend;
import com.video.commons.task.TaskStatusUtil;
import com.video.entity.ClientMsg;
import com.video.entity.Task;
import com.video.entity.Webcon;
import com.video.netty.send.*;
import com.video.service.client.IClientService;
import com.video.service.task.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private ITaskService taskService;

    @Autowired
    private IClientService clientService;

    @Autowired
    private TaskStatusUtil taskStatusUtil;


    /**
     * 添加任务 - 任务类型 1 - 定时音乐 2 - 定时采集 3 - 消防报警 4 - 语音合成 5 - 实时音乐
     * @param task
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskAddSend.class)
    public ResultData insert(Task task, @LoginUser Webcon webcon){
        task.setTaskid(UUID.randomUUID().toString());
        task.setAccount(webcon.getAccount());
        task.setTaskpriority(webcon.getPriority());
        taskService.insert(task);
        return new ResultData().setData(task);
    }

    /**
     * 修改任务
     * @param task
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskUpdateSend.class)
    public ResultData update(Task task){
        Task tasks = taskService.updateTaskByTaskId(task);
        return new ResultData().setData(tasks);
    }


    /**
     * 根据任务id查询任务
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTask")
    public ResultData queryTask(String taskid){
        Task task = taskService.getByTaskId(taskid);
        return new ResultData().setData(task);
    }

    /**
     * 获取任务列表 - taskt任务类型， webcon谁的任务
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    @MustLogin
    public ResultData<List<Task>> list(int taskt, String solution, @LoginUser Webcon webcon){
        System.out.println("查询任务列表！");
        List<Task> tasks = taskService.queryTaskList(taskt, webcon.getAccount(), solution);
        return new ResultData<List<Task>>().setData(tasks);
    }

    /**
     * 是否执行
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/isaction")
    public ResultData<Boolean> isAction(String taskid){
        boolean flag = taskStatusUtil.isAction(taskid);
        return new ResultData<Boolean>().setData(flag);
    }

    /**
     * 获得任务信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/info")
    public ResultData<Map<String, Object>> getTaskInfo(int tid){

        Map<String, Object> resultMap = new HashMap<>();

        //查询任务
        Task task = taskService.getById(tid);
        //根据任务获得音乐列表
        String mp3 = task.getMp3();
        resultMap.put("mp3", mp3 == null ? null : JSONArray.parseArray(mp3, String.class));

        //根据任务获得终端列表
        List<ClientMsg> clientMsgs = new ArrayList<>();
        String clients = task.getUsers();
        if(clients != null){
            List<Integer> cids = JSONArray.parseArray(clients, Integer.class);
            for (Integer cid : cids) {
                ClientMsg clientMsg = clientService.getById(cid);
                if(clientMsg != null) {
                    //如果终端名称为空 就显示ID号
                    clientMsg.setTerminalname(clientMsg.getTerminalname() == null ? clientMsg.getUserid() + "" : clientMsg.getTerminalname());
                    clientMsgs.add(clientMsg);
                }
            }
            resultMap.put("clients", clientMsgs);
        } else {
            resultMap.put("clients", null);
        }

        //设置类型
        resultMap.put("looptype", task.getLooptype());
        //设置音量
        resultMap.put("volume", task.getVolume());
        return new ResultData<Map<String, Object>>().setData(resultMap);
    }

    /**
     * 删除任务
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @SocketSend(params = {"#taskid", "#taskt"}, sendClass = TaskDeleteSend.class)
    @MustLogin
    public ResultData delete(String taskid, int taskt){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("taskid", taskid);
        boolean result = taskService.remove(queryWrapper);

        //删除任务状态信息
        taskStatusUtil.delTaskStatus(taskid);

        return new ResultData().setData(result);
    }

    /**
     * 拷贝任务
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/copy")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskCopySend.class)
    public ResultData copy(String taskid){
        Task task = taskService.copyTask(taskid);
        return new ResultData().setData(task);
    }

    /**
     * 手动执行任务
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/action")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskActionSend.class)
    public ResultData action(String taskid){
        Task task = taskService.getByTaskId(taskid);
        taskStatusUtil.actionTask(taskid);
        return new ResultData().setData(task);
    }

    /**
     * 手动暂停
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/pause")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskPauseSend.class)
    public ResultData pause (String taskid){
        Task task = taskService.getByTaskId(taskid);
        return new ResultData().setData(task);
    }

    /**
     * 手动停止
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/stop")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskStopSend.class)
    public ResultData stop(String taskid){
        Task task = taskService.getByTaskId(taskid);
        taskStatusUtil.stopTask(taskid);
        return new ResultData().setData(task);
    }

    /**
     * 上一曲
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/pre")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskPreSend.class)
    public ResultData pre(String taskid){
        Task task = taskService.getByTaskId(taskid);
        return new ResultData().setData(task);
    }

    /**
     * 下一曲
     * @param taskid
     * @return
     */
    @ResponseBody
    @RequestMapping("/next")
    @MustLogin
    @SocketSend(params = "#result", sendClass = TaskNextSend.class)
    public ResultData next(String taskid){
        Task task = taskService.getByTaskId(taskid);
        return new ResultData().setData(task);
    }

    /**
     * 修改任务音量
     * @return
     */
    @RequestMapping("/updateVolume")
    @ResponseBody
    @MustLogin
    @SocketSend(sendClass = TaskVolumeSend.class, params = {"#result", "#volume"})
    public ResultData updateVolume(String taskid, int volume, Integer looptype){

        if (taskStatusUtil.isAction(taskid)){
            Task task = taskService.updateTaskVolume(taskid, volume, looptype);
            return new ResultData().setData(task);
        } else {
            return new ResultData().setCode(ResultData.ResultCodeList.ERROR).setMsg("当前任务未执行,不能修改音量！");
        }
    }

    /**
     * 查询任务执行状态
     * @return
     */
    @RequestMapping("/queryTaskStatus")
    @ResponseBody
    @RequestUnLog
    public ResultData<List<Map<String, Object>>> queryTaskStatus(@RequestParam("taskids[]") String[] taskids){
        List<Map<String, Object>> status = taskStatusUtil.queryTaskStatus(taskids);
        return new ResultData<List<Map<String, Object>>>().setData(status);
    }

    /**
     * 查询终端执行状态
     * @return
     */
    @RequestMapping("/queryClientStatus")
    @ResponseBody
    @RequestUnLog
    public ResultData<List<Map<String, Object>>> queryClientStatus(@RequestParam("uids[]") String[] uids){
        List<Map<String, Object>> status = taskStatusUtil.queryClientsStatus(uids);
        return new ResultData<List<Map<String, Object>>>().setData(status);
    }
}
